package base;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

/**
 *  Maintains the information of a base.Coffee catalog. Contains a
 *  collection of {@link Product} objects.
 *  @author 第10组
 *  @version
 *  @see Product
 */
public class Catalog {
    /* Collection of base.Product objects.*/
    private Vector<Product> products = new Vector<Product>();
    /**
     * Default constructor
     */
    public Catalog() {

    }

    /**
     * Read products from file
     * @author tuxiaobei
     * @param path datafile path
     */
    public Catalog(String path) {
        File file = new File(path); //通过文件路径获取文件对象
        try {
            Scanner reader = new Scanner(file); //从文件对象中进行读取
            while (reader.hasNextLine()) {
                String str = reader.nextLine();
                String[] product_param = str.split(","); //用逗号进行分割
                try {
                    switch (product_param[0].charAt(0)) { //通过编号的首字母判断产品类型
                        case 'A':
                            addProduct(new Coffee_accessory(product_param));
                            break;
                        case 'B':
                            addProduct(new Coffee_brewer(product_param));
                            break;
                        case 'C':
                            addProduct(new Coffee(product_param));
                            break;
                    }
                } catch (ArrayIndexOutOfBoundsException e) {
                    System.out.println("error" + str);
                    e.printStackTrace();
                }
            }
            reader.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * Returns an iterator over the items in this catalog.
     *
     * return  an {@link Iterator}
     */
    public Iterator<Product> getItemsIterator() {

        return products.iterator();
    }





    /**
     * Adds a {@link Product} object to this catalog.
     *
     * @param newProduct  the {@link Product} object.
     */
    public void addProduct(Product newProduct) {
        products.add(newProduct);
    }

    /**
     * Returns the {@link Product} object with the specified
     * <code>code</code>.
     *
     * @param code  the code of an item.
     * @return  The {@link Product} object with the specified
     *          code. Returns <code>null</code> if the object with
     *          the code is not found.
     */
    public Product getProduct(String code) {
        for (Iterator<Product> i = getItemsIterator(); i.hasNext();) {
            Product nowProduct = i.next();
            if (nowProduct.getCode().equals(code)) {
                return nowProduct;
            }
        }
        return null;
    }

    /**
     * Returns the number of items in the catalog.
     *
     * @return the number of {@link Product} objects in this catalog.
     */
    public int getNumber() {
        return products.size();

    }

}